// 获取当前日期，用于初始化picker的默认值
function getCurrentDate() {
  const date = new Date();
  return [date.getFullYear(), date.getMonth() + 1, date.getDate()];
}

Page({
  data: {
    categories: [
      { id: 1, name: '餐饮', },
      { id: 2, name: '交通',  },
      { id: 3, name: '服饰', },
      { id: 4, name: '购物',  },
      { id: 5, name: '服务', },
      { id: 6, name: '教育',  },
      { id: 7, name: '娱乐', },
      { id: 8, name: '运动',  },
      { id: 9, name: '生活缴费', },
      { id: 10, name: '旅行',  },
      { id: 11, name: '宠物',  },
      { id: 12, name: '医疗', },
      { id: 13, name: '保险',  },
      { id: 14, name: '公益',  },
      { id: 15, name: '发红包', },
      { id: 16, name: '转账', },
      { id: 17, name: '亲属卡', },
      { id: 18, name: '其他', },
      { id: 19, name: '退还', },
      { id: 20, name: '生意', },
      { id: 21, name: '工资', },
      { id: 22, name: '奖金', },
      { id: 23, name: '其他人情', },
      { id: 24, name: '收红包', },
      { id: 25, name: '收转账', },
      { id: 26, name: '商家转账', },
      { id: 27, name: '退款', },
      { id: 28, name: '其他', },
      { id: 29, name: '理财', },
      { id: 30, name: '借还款', },
      { id: 31, name: '赠礼', },
      { id: 32, name: '其他', },
    ],
    selectedCategory: '请选择',
    categoryIndex: 0,
    multiDateOptions: [
      Array.from({ length: 2100 }, (_, i) => i + 0), // 年份
      Array.from({ length: 12 }, (_, i) => i + 0), // 月份
      Array.from({ length: 31 }, (_, i) => i + 0), // 日期
    ],
    selectedDate: getCurrentDate(), // 初始化为当前日期
    totalIncome: 0,
    totalExpense: 0,
    details: []
  },

  onLoad: function () {
    const { records, totalIncome, totalExpense } = loadRecords();
    this.setData({
      details: records,
      totalIncome: totalIncome,
      totalExpense: totalExpense
    });
  },
  

  bindDateChange: function (e) {
    const value = e.detail.value;
    this.setData({
      selectedDate: value
    });
  },

  bindPickerChange: function (e) {
    const index = e.detail.value;
    const selectedCategory = this.data.categories[index].name;
    this.setData({
      selectedCategory: selectedCategory,
      categoryIndex: index
    });
  },

  bindTypeChange: function (e) {
    const type = e.detail.value;
    this.setData({
      type: type
    });
  },

  handleSubmit: function (e) {
    const formData = e.detail.value;
    const type = formData.type === '收入' ? 'income' : '支出';
    const amount = parseFloat(formData.amount.replace('￥', '').replace('$', ''));

    const newRecord = {
      date: this.data.multiDateOptions[0][this.data.selectedDate[0]] + '-' +
            this.data.multiDateOptions[1][this.data.selectedDate[1]] + '-' +
            this.data.multiDateOptions[2][this.data.selectedDate[2]],
      amount: amount,
      description: formData.description,
      category: this.data.selectedCategory,
      type: type
    };

    this.saveToLocal(newRecord);
  },

  saveToLocal: function(formData) {
    let records = wx.getStorageSync('records') || [];
    records.push(formData);
    wx.setStorageSync('records', records);

    let totalIncome = 0;
    let totalExpense = 0;
    records.forEach(record => {
      if (record.type === 'income') {
        totalIncome += parseFloat(record.amount);
      } else {
        totalExpense += parseFloat(record.amount);
      }
    });

    wx.setStorageSync('totalIncome', totalIncome);
    wx.setStorageSync('totalExpense', totalExpense);

    this.setData({
      details: records,
      totalIncome: totalIncome,
      totalExpense: totalExpense
    });

    wx.showToast({
      title: '记账成功',
      icon: 'success'
    });

    setTimeout(() => {
      wx.navigateBack();
    }, 1500); // 延时1.5秒返回上一页
  }
});

function loadRecords() {
  const records = wx.getStorageSync('records') || [];
  const totalIncome = wx.getStorageSync('totalIncome') || 0;
  const totalExpense = wx.getStorageSync('totalExpense') || 0;
  return { records, totalIncome, totalExpense };
}